package zhihui.mobile.ui.main;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zgm.utils.DensityUtil;
import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.UiHeaderLayout;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.message.MessageInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.main.IMainContract;
import zhihui.mobile.presenter.main.MainPresenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.express.ExpressActivity;
import zhihui.mobile.ui.utils.UiFooterLayout;

/**
 * Created by 张公铭 on 2017/6/27 0027.
 */
public class MainActivity extends BaseActivity implements IMainContract.IMainView
{
    private MainPresenter mainPresenter = new MainPresenter();
    private UiHeaderLayout mHeader;
    private PullToRefreshScrollView sv_root;
    private ImageView iv_guard;
    private ImageView iv_house;
    private ImageView iv_packgage;
    private ImageView iv_code;
    private LinearLayout ll_message;
    private UiFooterLayout mFooter;

    private static final int MY_PERMISSION_REQUEST_CODE = 10000;
    private boolean headerChange = false;

    @Override
    protected int getLayoutResId()
    {
        return R.layout.main_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{mainPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        mainPresenter.init(this, this);
    }


    @Override
    public void initView()
    {
        mHeader = (UiHeaderLayout) findViewById(R.id.main_header);
        mHeader.setTitle("智惠妈妈");
        mHeader.setTitleColor("#ffffff");
        mHeader.hindRightTwoButton();
        mHeader.setRootBackgroundColor("#00000000");
        mHeader.hindLeftButton();
        mHeader.setRightOneBottonIcon(R.drawable.home_scan);
        mHeader.setRightOneButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                showMessage("敬请期待！");
                //扫一扫
                //Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                //startActivity(intent);
            }
        });
        mHeader.hindRightTwoButton();
        mHeader.bringToFront();

        //判断当前设备版本号是否为4.4以上，如果是，则通过调用setTranslucentStatus让状态栏变透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            setTranslucentStatus(true);
            mHeader.setBarHeight(UiUtils.getStatusBarHeight(this));
        }

        sv_root = (PullToRefreshScrollView) findViewById(R.id.sv_main_root);
        sv_root.setOnScrollChangedListener(new PullToRefreshScrollView.IOnScrollChangedListener()
        {
            @Override
            public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy)
            {
                Log.i("xxx", "scrollX" + x + "----scrollY" + y);
                int maxHeight = DensityUtil.dip2px(MainActivity.this, 70);
                if (y > maxHeight && !headerChange)
                {
                    headerChange = true;
                    changeHeaderBlack();
                }
                if(y<maxHeight && headerChange)
                {
                    headerChange = false;
                    changeHeaderTranc();
                }
            }
        });
        sv_root.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        sv_root.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>()
        {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView)
            {
                mainPresenter.refreshMessage();
            }
        });

        iv_guard = (ImageView) findViewById(R.id.iv_main_activity_menjin);
        iv_guard.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //门禁
                showMessage("敬请期待");
            }
        });

        iv_house = (ImageView) findViewById(R.id.iv_main_activity_house);
        iv_house.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //只能家居
                showMessage("敬请期待");
            }
        });

        iv_packgage = (ImageView) findViewById(R.id.iv_main_activity_package);
        iv_packgage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //包裹
                if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(MainActivity.this))
                {
                    return;
                }
                MainActivity.this.startActivity(new Intent().setClass(MainActivity.this, ExpressActivity.class));
            }
        });

        iv_code = (ImageView) findViewById(R.id.iv_main_activity_code);
        iv_code.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //取货码
                if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(MainActivity.this))
                {
                    return;
                }
                MainActivity.this.startActivity(new Intent().setClass(MainActivity.this, ExpressActivity.class));
            }
        });

        ll_message = (LinearLayout) findViewById(R.id.ll_main_activity_message);

        mFooter = (UiFooterLayout) findViewById(R.id.main_footer);
        mFooter.change(UiFooterLayout.Meun.Main);
    }

    private void changeHeaderBlack()
    {
        mHeader.setTitleColor("#333333");
        mHeader.setRootBackgroundColor("#fafafa");
        mHeader.setRightOneBottonIcon(R.drawable.home_scan_pull_up);
    }

    private void changeHeaderTranc()
    {
        mHeader.setTitleColor("#ffffff");
        mHeader.setRootBackgroundColor("#00000000");
        mHeader.setRightOneBottonIcon(R.drawable.home_scan);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on)
    {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on)
        {
            winParams.flags |= bits;
        } else
        {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    @Override
    public void requestPermissions(String... permissions)
    {
        ActivityCompat.requestPermissions(this, permissions, MY_PERMISSION_REQUEST_CODE);
    }

    @Override
    public void refreshMessage(List<MessageInfo> list)
    {
        ll_message.removeAllViews();
        for (int i = 0; i < list.size(); i++)
        {
            final MessageInfo messageInfo = list.get(i);
            if (messageInfo == null)
            {
                continue;
            }
            View view = LayoutInflater.from(this).inflate(R.layout.main_message_item, ll_message, false);
            ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_main_message_item_icon);
            switch (messageInfo.getType_f())
            {
                case 1:
                case 2:
                case 3:
                    iv_icon.setImageResource(R.drawable.package_icon);
                    break;
                case 6:
                    iv_icon.setImageResource(R.drawable.ticket_icon);
                    break;
                case 7:
                    iv_icon.setImageResource(R.drawable.order_icon);
                    break;
                case 8:
                    iv_icon.setImageResource(R.drawable.sales_icon);
                    break;
                case 9:
                    iv_icon.setImageResource(R.drawable.system_icon);
                    break;
            }

            TextView tv_title = (TextView) view.findViewById(R.id.tv_main_message_item_title);
            tv_title.setText(messageInfo.getTitle_f());
            TextView tv_time = (TextView) view.findViewById(R.id.tv_main_message_item_time);
            tv_time.setText(messageInfo.getCreatetime_f());
            TextView tv_desc = (TextView) view.findViewById(R.id.tv_main_message_item_desc);
            tv_desc.setText(messageInfo.getContent_f());
            view.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    goMessageByType(messageInfo.getType_f());
                }
            });
            ll_message.addView(view);
        }
    }

    @Override
    public void onRefreshComplete()
    {
        sv_root.onRefreshComplete();
    }

    private void goMessageByType(int type)
    {
        switch (type)
        {
            case 1:
                Intent intent = new Intent(this, ExpressActivity.class);
                intent.putExtra("type", 0);     //收到的包裹
                startActivity(intent);
                break;
            case 2:
                Intent intent1 = new Intent(this, ExpressActivity.class);
                intent1.putExtra("type", 1);
                startActivity(intent1);
                break;
            case 4:
                mainPresenter.goMySales(this, this);
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSION_REQUEST_CODE)
        {
            boolean isAllGranted = true;
            for (int grant : grantResults)
            {
                if (grant != PackageManager.PERMISSION_GRANTED)
                {
                    isAllGranted = false;
                    break;
                }
            }

            if (isAllGranted)
            {
                mainPresenter.initCrahAndLog();
            } else
            {
                UiUtils.showPermission(this, "智惠妈妈需要访“定位”、“相机”、“外部存储器”、“手机状态”和“文件权限”，请到 “应用信息 -> 权限” 中授予！否则此应用无法使用！", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        finish();
                    }
                });
            }
        }
    }
}
